<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>中因医院/系统管理/公告管理</title>
    <meta http-equiv="Access-Control-Allow-Origin" content="*"/>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="shortcut icon" href="/images/hospital.png">
</head>
<body>
<!-- 搜索区域 -->
<div class="searchAnnouncement">
    标题：
    <div class="layui-inline">
        <input class="layui-input" name="name" id="name" autocomplete="off"/>
    </div>
    <button class="layui-btn" id="searchBtn" data-type="reload">搜索</button>
</div>
<!-- 表格 -->
<table id="demo" lay-filter="demo"></table>
<!-- 定义了表头（新增、选项、打印、导出） -->
<div id="toolbar" style="display: none;">
    <div class="layui-btn-group">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
    </div>
</div>
<!-- 定义操作栏(修改、删除) -->
<script type="text/html" id="opeartebar">
    <a class="layui-btn layui-btn-sm" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</a>
</script>
<!--定义模态框-->
<div id="modal" style="display:none;">
    <form class="layui-form" lay-filter="entity">
        <input type="text" name="id" class="layui-input" style="display:none">
        <div class="layui-form-item">
            <label class="layui-form-label">公告标题：</label>
            <div class="layui-input-inline layui-col-md4">
                <input type="text" name="title" required lay-verify="required" placeholder="公告标题" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公告内容：</label>
            <div class="layui-input-block">
                <textarea placeholder="公告内容" name="contents" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">公告状态：</label>
            <div class="layui-input-inline">
                <input type="radio" name="annStatus" value="0" title="正常" checked="">
                <input type="radio" name="annStatus" value="1" title="废弃">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="add">立即提交</button>
                <button style="display: none;" type="reset" class="layui-btn layui-btn-primary swiper-right-form-reset">
                    重置
                </button>
            </div>
        </div>
    </form>
</div>
<script src="/layui/layui.js"></script>
<script>
    layui.use(['table', 'layer', 'form', 'jquery'], function () {
        var layer = layui.layer;
        var form = layui.form;
        var $ = layui.jquery;
        var table = layui.table;
        table.render({
            elem: '#demo'
            , id: 'testReload'
            , height: 472
            , url: '/api/announcement/page' //数据接口
            , page: true //开启分页
            , cols: [[ //表头
                {field: 'id', title: 'ID', sort: true, fixed: 'left'}
                , {field: 'title', title: '标题'}
                , {field: 'contents', title: '内容'}
                , {field: 'createDatetime', title: '创建时间'}
                , {
                    field: 'annStatus', title: '状态', templet: function (d) {
                        return d.annStatus === 0 ? "正常" : "废弃";
                    }
                }
                , {field: 'annDate', title: '修改时间'}
                , {title: '操作', toolbar: '#opeartebar', align: 'center'}
            ]]
            , toolbar: '#toolbar'
            , response: {
                statusCode: 1
            }
            , parseData: function (res) {
                if (res.status == 1) {
                    return {
                        "code": res.status,
                        "msg": res.message,
                        "count": res.total,
                        "data": res.data
                    }
                }
            }
        });
        table.on('toolbar(demo)', function (obj) {
            switch (obj.event) {
                case 'add' :
                    // 打开模态框
                    open2("新增公告", null);
                    break;
            }
        });
        table.on('tool(demo)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'delete' :
                    layer.confirm("确定要删除该条信息吗？", function (index) {
                        // ajax 将要删除的id传到服务器
                        $.ajax({
                            url: '/api/announcement/' + data.id,
                            type: 'delete',
                            dataType: 'json',
                            success: function (res) {
                                if (res.status == 1) {
                                    layer.msg(res.message);
                                    // 重新加载
                                    //active.reload();
                                    obj.del();
                                } else {
                                    layer.msg(res.message);
                                }
                            }
                        });
                        // 关闭对话框
                        layer.close(index);
                    });
                    break;
                case 'edit':
                    open2("修改公告", data);
                    break;
            }
        });
        var active = {
            reload: function () {
                var name = $('#name');
                table.reload('testReload', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        "title": name.val()
                    }
                });
            }
        };

        function open2(title, entity) {
            form.val("entity", entity);
            layer.open({
                type: 1
                , title: title
                , content: $("#modal")
                , offset: '10px'
                , area: ['600px', '80%']
                , end: function () {
                    table.reload("testReload");
                }
            });
        }

        form.on('submit(add)', function (data) {
            // 获取表单中的值
            var data1 = form.val("entity");
            var type = data1.id ? "put" : "post";
            // 将获取到的数据发送到服务器端
            $.ajax({
                url: '/api/announcement',
                type: type,
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(data1),
                success: function (res) {
                    if (res.status == 1) {
                        layer.msg(res.message, {time: 1 * 1000}, function () {
                            layer.closeAll();
                        });
                    } else {
                        layer.msg(res.message);
                    }
                },
                error: function () {
                    layer.msg("123");
                }
            });
            return false;
        })
        $(".searchAnnouncement .layui-btn").on("click", function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>